package com.itheima.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.BlackListApi;
import com.itheima.api.QuestionApi;
import com.itheima.api.SettingsApi;
import com.itheima.domain.BlackList;
import com.itheima.domain.Question;
import com.itheima.domain.Settings;
import com.itheima.domain.UserInfo;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.vo.PageResult;
import com.itheima.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;

    /**
     * 查询通知设置
     * @return
     */
    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.getMobile();
        //设置用户id和手机号
        settingsVo.setId(userId);
        settingsVo.setPhone(mobile);
        //获取用户设置问题
        Question question = questionApi.findByUserId(userId);
        //设置默认值
        String questTxt = question == null ? "你jb谁？" : question.getTxt();
        settingsVo.setStrangerQuestion(questTxt);
        //查询设置开关
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null){
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }

    /**
     * 设置陌生人问题
     * @param content
     */
    public void saveQuestion(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        if (question == null){
            //执行save操作
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            //执行update操作
            question.setTxt(content);
            questionApi.update(question);
        }

    }


    /**
     * 设置那啥
     * @param map
     */
    public void notifications(Map map) {
        boolean likeNotification = (boolean)map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);

        if (settings == null){
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);
        }else {
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }

    }

    /**
     * 查询黑名单
     * @param page
     * @param size
     * @return
     */
    public PageResult page(int page, int size) {
        Long userId = UserHolder.getUserId();
        IPage<UserInfo> pageInfo = blackListApi.getPage(userId,page,size);
        return new PageResult(page,size, (int) pageInfo.getTotal(),pageInfo.getRecords());
    }

    /**
     * 移除黑名单
     * @param uid
     */
    public void deleteBlackList(Long uid) {
        Long userId = UserHolder.getUserId();
        blackListApi.deleteBlackList(userId,uid);
    }


}
